package com.point.community;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.demo.PmsBilInfoInterface;
import com.alibaba.dubbo.demo.bean.PmsBilInfo;
import com.alibaba.dubbo.demo.bean.PageBean;
import com.alibaba.dubbo.demo.provider.utils.CommUtils;
import com.alibaba.dubbo.demo.provider.utils.VeDate;
import com.point.community.bean.dwzPageData;
import net.sf.json.JSONObject;

public class PmsBilInfoAction extends JsonAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Logger logger = Logger.getLogger(PmsBilInfoAction.class);
	
	private String msg = "";
	private boolean success = false;
	private PageBean pageBean;
	private String statusCode,message,navTabId,rel,callbackType,forwardUrl,confirmMsg;
	
	private PmsBilInfo pmsBilInfo = new PmsBilInfo();
	private List<PmsBilInfo> pmsBilInfoList = new ArrayList<PmsBilInfo>();
	private Map<String,Object> data = new HashMap<String,Object>();
	
	@Autowired
	PmsBilInfoInterface pmsBilInfoService;

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNavTabId() {
		return navTabId;
	}

	public void setNavTabId(String navTabId) {
		this.navTabId = navTabId;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getCallbackType() {
		return callbackType;
	}

	public void setCallbackType(String callbackType) {
		this.callbackType = callbackType;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public String getConfirmMsg() {
		return confirmMsg;
	}

	public void setConfirmMsg(String confirmMsg) {
		this.confirmMsg = confirmMsg;
	}

	public PmsBilInfo getPmsBilInfo() {
		return pmsBilInfo;
	}

	public void setPmsBilInfo(PmsBilInfo pmsBilInfo) {
		this.pmsBilInfo = pmsBilInfo;
	}

	public List<PmsBilInfo> getPmsBilInfoList() {
		return pmsBilInfoList;
	}

	public void setPmsBilInfoList(List<PmsBilInfo> pmsBilInfoList) {
		this.pmsBilInfoList = pmsBilInfoList;
	}

	public PmsBilInfoInterface getPmsBilInfoService() {
		return pmsBilInfoService;
	}

	public void setPmsBilInfoService(PmsBilInfoInterface pmsBilInfoService) {
		this.pmsBilInfoService = pmsBilInfoService;
	} 
	public Map<String,Object> getData() {
		return data;
	}
	public void setData(Map<String,Object> data) {
		this.data = data;
	}	
	public String queryAllPmsBilInfo(){
		String currentPage = request.getParameter("pageNum");
		String numPerPage = request.getParameter("numPerPage");
		if(StringUtils.isEmpty(currentPage)){
			currentPage = "1";
	    }
	    if(StringUtils.isEmpty(numPerPage)){
	    	numPerPage = "20";
	    }
	    int nCurrentPage =  Integer.parseInt(currentPage);    
	    int nNumPerPage =  Integer.parseInt(numPerPage);  
	    int nStart = (nCurrentPage - 1)*nNumPerPage;
		String start = Integer.toString(nStart);

		pageBean = new PageBean(start, numPerPage);
		
		pageBean = pmsBilInfoService.queryAllPmsBilInfo(pageBean,pmsBilInfo);
		pmsBilInfoList = pageBean.getResultList();
		
		request.getSession().setAttribute("pmsBilInfoList", pmsBilInfoList);
		request.getSession().setAttribute("totalCount", pageBean.getTotal());
		request.getSession().setAttribute("numPerPage", numPerPage);
		request.getSession().setAttribute("currentPage", currentPage);

		
		dwzPageData dwz = new dwzPageData();
		dwz.currentPage = currentPage;
		dwz.numPerPage = numPerPage;
		dwz.totalCount = pageBean.getTotal();
		
		request.getSession().setAttribute("pmsBilInfoDwzData", dwz);
		
		return "pmsbilinfo";
	}
	
	public String queryOnePmsBilInfo()
	{
		long id =  getParameterLong("id");
		PmsBilInfo item = pmsBilInfoService.queryOnePmsBilInfo(id);
		request.getSession().setAttribute("pmsBilInfo", item);
		return "onepmsbilinfo";
	}	
	
	public String savePmsBilInfo()
	{
		 pmsBilInfo = new PmsBilInfo();
		 //构造请求数据
		 //		//ID
		Long id = getParameterLong("id");
		pmsBilInfo.setId(id);
		//订单编号
		String billcode = getParameterString("billcode");
		pmsBilInfo.setBillcode(billcode);
		//日期
		String billdate = getParameterString("billdate");
		pmsBilInfo.setBilldate(billdate);
		//订单数量
		Integer machinenum = getParameterInteger("machinenum");
		pmsBilInfo.setMachinenum(machinenum);
		//操作员
		String operater = getParameterString("operater");
		pmsBilInfo.setOperater(operater);
		//电话
		String telphone = getParameterString("telphone");
		pmsBilInfo.setTelphone(telphone);

		 
		boolean ret = pmsBilInfoService.savePmsBilInfo(pmsBilInfo);
		if (ret) 
		   {
				success = true;
				logger.info("保存订单信息成功!");
				
				statusCode="200";
				message="操作成功";
				navTabId="pmsbilinfo";
				rel="";
				callbackType="closeCurrent";
				forwardUrl="";
				confirmMsg="";
			}
		   else
		   {
			    success = false;
				logger.info("保存订单信息异常!");
				
				statusCode="200";
				message="操作失败";
				navTabId="pmsbilinfo";
				rel="";
				callbackType="closeCurrent";
				forwardUrl="";
				confirmMsg="";
		   }
		msg = "保存订单信息异常！";
		request.getSession().setAttribute("errMsg", msg);
		return SUCCESS;
	}
	
	
	public String updatePmsBilInfo()
	{
		 Long idParam = getParameterLong("id");		
		 pmsBilInfo = pmsBilInfoService.queryOnePmsBilInfo(idParam);
		 //构造请求数据
		//		//ID
		Long id = getParameterLong("id");
		pmsBilInfo.setId(id);
		//订单编号
		String billcode = getParameterString("billcode");
		pmsBilInfo.setBillcode(billcode);
		//日期
		String billdate = getParameterString("billdate");
		pmsBilInfo.setBilldate(billdate);
		//订单数量
		Integer machinenum = getParameterInteger("machinenum");
		pmsBilInfo.setMachinenum(machinenum);
		//操作员
		String operater = getParameterString("operater");
		pmsBilInfo.setOperater(operater);
		//电话
		String telphone = getParameterString("telphone");
		pmsBilInfo.setTelphone(telphone);

		boolean ret = pmsBilInfoService.updatePmsBilInfo(pmsBilInfo);
		if (ret) {
			success = true;
			logger.info("更新订单信息成功!");

			statusCode = "200";
			message = "操作成功";
			navTabId = "pmsbilinfo";
			rel = "";
			callbackType = "closeCurrent";
			forwardUrl = "";
			confirmMsg = "";
		} else {
			success = false;
			logger.info("更新订单信息异常!");

			statusCode = "200";
			message = "操作失败";
			navTabId = "pmsbilinfo";
			rel = "";
			callbackType = "closeCurrent";
			forwardUrl = "";
			confirmMsg = "";
			msg = "更新订单信息异常！";
			request.getSession().setAttribute("errMsg", msg);
		}
		return SUCCESS;
	}
	
	public String deletePmsBilInfo()
	{
		Long id = getParameterLong("id");	
		pmsBilInfo.setId(id);
		boolean ret = pmsBilInfoService.deletePmsBilInfo(pmsBilInfo);
		if (ret) {
			success = true;
			logger.info("更新订单信息成功!");

			statusCode = "200";
			message = "操作成功";
			navTabId = "pmsbilinfo";
			rel = "";
			callbackType = "closeCurrent";
			forwardUrl = "";
			confirmMsg = "";
		} else {
			success = false;
			logger.info("更新订单信息异常!");

			statusCode = "200";
			message = "操作失败";
			navTabId = "pmsbilinfo";
			rel = "";
			callbackType = "closeCurrent";
			forwardUrl = "";
			confirmMsg = "";
			msg = "更新订单信息异常！";
			request.getSession().setAttribute("errMsg", msg);
		}
		return SUCCESS;
	}
	
	public String setPmsBilInfoValid()
	{
		Long id = getParameterLong("id");		
		pmsBilInfo = pmsBilInfoService.queryOnePmsBilInfo(id);
		int valid = getParameterInt("valid");	
		//pmsBilInfo.setIsvalid(valid);
		boolean ret = pmsBilInfoService.updatePmsBilInfo(pmsBilInfo);
		if (ret) 
		   {
				success = true;
				logger.info("更新订单信息成功!");
				
				statusCode="200";
				message="操作成功";
				navTabId="pmsbilinfo";
				rel="";
				callbackType="";
				forwardUrl="";
				confirmMsg="";
			}
		   else
		   {
			    success = true;
				logger.info("更新订单信息异常!");
				
				statusCode="200";
				message="操作失败";
				navTabId="pmsbilinfo";
				rel="";
				callbackType="";
				forwardUrl="";
				confirmMsg="";
		   }
		return SUCCESS;
	}
	
}